package com.ficus.road.maintain.converter;


import com.ficus.road.maintain.core.model.Hit;
import com.ficus.road.maintain.core.model.smart.SmartHit;
import com.ficus.road.maintain.core.model.smartnew.SmartRoadSection;
import com.ficus.road.maintain.dao.smartnew.SmartRoadSectionDao;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

@Component
@RequiredArgsConstructor
public class SmartHitConverter {

    private static Map<String, String> roadSmartIdNameMap = new ConcurrentHashMap<>();
    private final SmartRoadSectionDao smartRoadSectionDao;

    public static SmartHit convert(Hit hit) {
        SmartHit smartHit = new SmartHit();
        smartHit.setId(hit.getId());
        smartHit.setImage(hit.getAttrs().getSceneImage());
        smartHit.setType(hit.getType());
        smartHit.setTime(hit.getFirstHitTime());
        smartHit.setLocation(hit.getAttrs().getRoadName());
        smartHit.setRoadDirection(hit.getAttrs().getRoadDirection());
        return smartHit;
    }

    @PostConstruct
    void init() {
        roadSmartIdNameMap = smartRoadSectionDao.findAll().stream().collect(Collectors.toMap(SmartRoadSection::getId, SmartRoadSection::getRoadName));
    }
}
